package com.warmheart.base.session;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.warmheart.base.config.RedisService;
import com.warmheart.base.config.SysConfig;
import com.warmheart.base.entity.Operator;
import com.warmheart.core.enums.PlatEnum;
import com.warmheart.core.util.UUIDGenerator;

/**
 * 
 * @ClassName: SessionManager
 * @Description: SESSION管理
 * @author ZhangJianyang
 * @date 2025-06-16 09:37:02
 *
 */
@Component
public class SessionManager {

    @Autowired(required = false)
    private RedisService redisService;

    @Autowired(required = false)
    private SysConfig sysConfig;

    // SESSION缓存前缀
    private final String SESSION_PRE_FIX = PlatEnum.RedisPrefix.PREFIX_APP.getValue() + "SESSION_";

    // API缓存前缀
    private final String API_PRE_FIX = PlatEnum.RedisPrefix.PREFIX_APP.getValue() + "API_";

    /**
     * @function 将当前操作用户数据存入REDIS
     * @param sessionId
     * @param operator
     * @return void
     */
    public String setOperator(Operator operator) {
        String sessionId = UUIDGenerator.getUUID();
        operator.setSessionId(sessionId);
        // 设置API对应SESSION有效性缓存
        redisService.set(API_PRE_FIX + operator.getLoginUser().getUserNo(), sessionId,
                sysConfig.getSecretMangerExpire());
        // 设置SESSION缓存
        redisService.set(SESSION_PRE_FIX + sessionId, operator, sysConfig.getSessionExpire());
        return sessionId;
    }

    /**
     * 获取用户对象
     * 
     * @param request
     * @param cookieType
     * @return
     */
    public Operator getOperator(HttpServletRequest request) {
        String sessionId = request.getHeader("X-Token");
        if (StringUtils.isBlank(sessionId)) {
            return null;
        }
        Operator currOperator = redisService.getObject(SESSION_PRE_FIX + sessionId, Operator.class);
        if (currOperator == null) {
            return null;
        }
        Object realSessionIdObj = redisService.get(API_PRE_FIX + currOperator.getLoginUser().getUserNo());
        if (realSessionIdObj == null) {
            return null;
        }
        String realSessionId = realSessionIdObj.toString();
        if (StringUtils.isBlank(realSessionId) || !realSessionId.equals(sessionId)) {
            return null;
        }
        return currOperator;
    }

    /**
     * @function 重设用户session（刷新）session失效日期以及SESSION有效性日期
     * @param request
     * @return
     * @return SessionUser
     */
    public Long resetOperator(HttpServletRequest request) {
        Operator operator = getOperator(request);
        if (operator != null) {
            redisService.expire(SESSION_PRE_FIX + operator.getSessionId(), sysConfig.getSessionExpire());
            redisService.expire(API_PRE_FIX + operator.getLoginUser().getUserNo(),
                    sysConfig.getSecretMangerExpire());
        }
        return null;
    }

    /**
     * 
     * @Title: setOperatorIdentity
     * @Description: 设置用户身份
     * @author ZhangJianyang
     * @param request
     * @param userIdentity
     * @return
     */
    public Long setOperatorIdentity(HttpServletRequest request, String userIdentity) {
        Operator operator = getOperator(request);
        if (operator != null) {
            operator.getLoginUser().setUserIdentity(userIdentity);
            redisService.set(SESSION_PRE_FIX + operator.getSessionId(), operator,
                    sysConfig.getSessionExpire());
            redisService.expire(API_PRE_FIX + operator.getLoginUser().getUserNo(),
                    sysConfig.getSecretMangerExpire());
        }
        return null;
    }

    /**
     * 
     * @Title: delOperator
     * @Description: 删除用户缓存
     * @author ZhangJianyang
     * @param request
     */
    public void delOperator(HttpServletRequest request) {
        Operator operator = getOperator(request);
        if (operator != null) {
            // 删除API对应session有效性
            redisService.del(API_PRE_FIX + operator.getLoginUser().getUserNo());
        }
    }
}
